-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [#521] User can custom recover when a request panic #103
Conversation
Important Review skippedAuto reviews are limited to specific labels. π·οΈ Labels to auto review (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #103 +/- ##
==========================================
- Coverage 79.29% 78.15% -1.14%
==========================================
Files 12 16 +4
Lines 874 1158 +284
==========================================
+ Hits 693 905 +212
- Misses 151 211 +60
- Partials 30 42 +12 β View full report in Codecov by Sentry. |
route.go
Outdated
} | ||
middlewares = append(defaultMiddlewares, middlewares...) | ||
r.setMiddlewares(middlewares) | ||
} | ||
|
||
func (r *Route) Recover(recoverFunc func(ctx *gin.Context, err interface{})) { | ||
r.instance.Use(func(ctx *gin.Context) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will we end up using gin.CustomRecovery?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it can be used directly, yes, we can.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is CustomRecovery
suitable, please?
service_provider.go
Outdated
@@ -35,13 +36,18 @@ func (receiver *ServiceProvider) Boot(app foundation.Application) { | |||
|
|||
if ConfigFacade = app.MakeConfig(); ConfigFacade == nil { | |||
color.Errorln(errors.ConfigFacadeNotSet.SetModule(module)) | |||
shutdownOnCriticalError("ConfigFacade is not set") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's unnecessary for now, we want to print a warning instead of interrupting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to remove the function totally.
can you give me a direction to solve the error? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty good now, could you add some test cases for the new logic?
@@ -44,11 +44,12 @@ func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.V | |||
request := contextRequestPool.Get().(*ContextRequest) | |||
httpBody, err := getHttpBody(ctx) | |||
if err != nil { | |||
log.Error(fmt.Sprintf("%+v", err)) | |||
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to modify this, I optimized the code in a previous PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please modify this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do I need to return log
?
patch-3 of my fork currently uses LogFacade
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log
is fine, please update your branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, please add some test cases for the new logic, then we can merge this.
@@ -44,11 +44,12 @@ func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.V | |||
request := contextRequestPool.Get().(*ContextRequest) | |||
httpBody, err := getHttpBody(ctx) | |||
if err != nil { | |||
log.Error(fmt.Sprintf("%+v", err)) | |||
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please modify this
I wrote two tests for a custom function and a default one. I need to create a new test file or add to an existing one (for example route_test.go). for the default case you need to use
|
You don't need to call |
#103 (comment) |
route_test.go
Outdated
"github.com/gin-gonic/gin/render" | ||
contractshttp "github.com/goravel/framework/contracts/http" | ||
"github.com/goravel/framework/contracts/validation" | ||
configmocks "github.com/goravel/framework/mocks/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRecoverWithCustomCallback(t *testing.T) { | ||
mockConfig := &configmocks.Config{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mockConfig := &configmocks.Config{} | |
mockConfig := configmocks.NewConfig(t) |
route.go
Outdated
} | ||
} | ||
|
||
func (r *Route) Recover(callback func(ctx context.Context, err any)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, mistake, should be:
func (r *Route) Recover(callback func(ctx context.Context, err any)) { | |
func (r *Route) Recover(callback func(ctx httpcontract.Context, err any)) { |
Please modify https://github.com/goravel/framework/pull/723/files#diff-37b488415e77368d5640d20902b15e5603864dc8c5c1e715bc36e2a8510b81f8R19 synchronously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case to test the custom recover as well.
}() | ||
|
||
select { | ||
case <-done: | ||
case <-ctx.Request().Origin().Context().Done(): | ||
if errors.Is(ctx.Request().Origin().Context().Err(), context.DeadlineExceeded) { | ||
case <-timeoutCtx.Done(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
"github.com/gin-gonic/gin/render" | ||
contractshttp "github.com/goravel/framework/contracts/http" | ||
"github.com/goravel/framework/contracts/validation" | ||
configmocks "github.com/goravel/framework/mocks/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRecoverWithCustomCallback(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case to test the default recover as well.
assert.Equal(t, http.StatusInternalServerError, w.Code) | ||
|
||
mockConfig.AssertExpectations(t) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test case is pretty good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, please create a fiber like this. π
And another information, we are going to release v1.15 next week. It would be great if we could complete this issue this week. |
π Description
Closes goravel/goravel#521
@coderabbitai summary
β Checks